package com.job.client.auth.service.impl;

import com.job.client.auth.pojo.Record;
import com.job.client.auth.service.RecordService;
import com.job.client.common.result.AppHttpCode;
import com.job.client.common.result.ResponseResult;
import com.job.client.common.utils.DateTimeUtils;
import com.job.feign.client.company.CompanyFeign;
import com.job.feign.client.posts.PostsFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.xml.soap.SAAJResult;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Async //异步
public class RecordServiceImpl implements RecordService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private PostsFeign postsFeign;
    @Autowired
    private CompanyFeign companyFeign;
    @Override
    public ResponseResult initRecord(String userId) {
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        Record record = new Record(userId);
        mongoTemplate.insert(record);
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult addPosts(String userId, String postsId) {
        if(userId == null || postsId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        Query query = new Query(Criteria.where("user_id").is(userId));
        Update update = new Update().push("posts_ids", postsId);
        mongoTemplate.findAndModify(query, update, Record.class);
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult addCompany(String userId, String companyId) {
        if(userId == null || companyId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        Query query = new Query(Criteria.where("user_id").is(userId));
        Update update = new Update().push("company_ids", companyId);
        mongoTemplate.findAndModify(query, update, Record.class);
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult addConnect(String userId, String postsId) {
        if(userId == null || postsId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(checkConnect(userId,postsId)){
            return ResponseResult.OkResult();
        }
        Query query = new Query(Criteria.where("user_id").is(userId));
        Update update = new Update().push("connect_posts_ids", postsId);
        mongoTemplate.findAndModify(query, update, Record.class);
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult deletePosts(String userId, String postsId) {
        if(userId == null || postsId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        Query query = new Query(Criteria.where("user_id").is(userId));
        Update update = new Update();
        update.pull("posts_ids",postsId);
        mongoTemplate.updateFirst(query,update,Record.class);
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult deleteCompany(String userId, String companyId) {
        if(userId == null || companyId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        Query query = new Query(Criteria.where("user_id").is(userId));
        Update update = new Update();
        update.pull("company_ids",companyId);
        mongoTemplate.updateFirst(query,update,Record.class);
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult checkPosts(String userId, String postsId) {
        Query query = new Query(Criteria.where("user_id").is(userId).and("posts_ids").in(postsId));
        Record one = mongoTemplate.findOne(query, Record.class);
        return ResponseResult.OkResult(one != null);
    }

    @Override
    public ResponseResult checkCompany(String userId, String companyId) {
        Query query = new Query(Criteria.where("user_id").is(userId).and("company_ids").in(companyId));
        Record one = mongoTemplate.findOne(query, Record.class);
        return ResponseResult.OkResult(one != null);
    }

    @Override
    public ResponseResult getPosts(String userId, Integer page, Integer size) {
        if(userId == null ){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(page == null){
            page = 1;
        }
        if(size == null){
            size = 10;
        }
        Query query = new Query(Criteria.where("user_id").is(userId));
        Record one = mongoTemplate.findOne(query, Record.class);
        if(one == null){
            return ResponseResult.ErrorResult(AppHttpCode.MONGODB_DATA_NULL);
        }
        List<String> postsIds = one.getPostsIds();
        // 实现分页查询
        Integer total = postsIds.size();
            // 整理分页数据
        int startIndex = (page - 1) * size;
        int endIndex = Math.min(startIndex + size, postsIds.size());
            // 实现分页
        List<String> pagePostsIds = postsIds.subList(startIndex, endIndex);
        ResponseResult result = postsFeign.getAllInfoList(pagePostsIds);
        if(result.getCode() != 200){
            return result;
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",total);
        data.put("postsInfoList",result.getData());

        return ResponseResult.OkResult(data);
    }

    @Override
    public ResponseResult getCompany(String userId, Integer page, Integer size) {
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(page == null){
            page = 1;
        }
        if(size == null){
            size = 10;
        }
        Query query = new Query(Criteria.where("user_id").is(userId));
        Record one = mongoTemplate.findOne(query, Record.class);
        if(one == null){
            return ResponseResult.ErrorResult(AppHttpCode.MONGODB_DATA_NULL);
        }
        List<String> companyIds = one.getCompanyIds();
        // 实现分页查询
        Integer total = companyIds.size();
        // 整理分页数据
        int startIndex = (page - 1) * size;
        int endIndex = Math.min(startIndex + size, companyIds.size());
        // 实现分页
        List<String> pageCompanyIds = companyIds.subList(startIndex, endIndex);
        ResponseResult result = companyFeign.getCompanyInfoByList(pageCompanyIds);
        if(result.getCode() != 200){
            return result;
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",total);
        data.put("companyInfoList",result.getData());

        return ResponseResult.OkResult(data);
    }

    @Override
    public ResponseResult getConnect(String userId, Integer page, Integer size) {
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(page == null){
            page = 1;
        }
        if(size == null){
            size = 10;
        }
        Query query = new Query(Criteria.where("user_id").is(userId));
        Record one = mongoTemplate.findOne(query, Record.class);
        if(one == null){
            return ResponseResult.ErrorResult(AppHttpCode.MONGODB_DATA_NULL);
        }
        List<String> connectPostsIds = one.getConnectPostsIds();
        // 实现分页查询
        Integer total = connectPostsIds.size();
        // 整理分页数据
        int startIndex = (page - 1) * size;
        int endIndex = Math.min(startIndex + size, connectPostsIds.size());
        // 实现分页
        List<String> pageConnectPostsIds = connectPostsIds.subList(startIndex, endIndex);
        ResponseResult result = postsFeign.getAllInfoList(pageConnectPostsIds);
        if(result.getCode() != 200){
            return result;
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",total);
        data.put("connectInfoList",result.getData());

        return ResponseResult.OkResult(data);
    }

    public Boolean checkConnect(String userId,String postsId){
        Query query = new Query(Criteria.where("user_id").is(userId).and("connect_posts_ids").in(postsId));
        Record one = mongoTemplate.findOne(query, Record.class);
        return one != null;
    }
}
